package com.decent.sms.entity;

import cn.hutool.core.util.ObjectUtil;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.afs.model.v20180112.AuthenticateSigRequest;
import com.aliyuncs.afs.model.v20180112.AuthenticateSigResponse;
import com.aliyuncs.exceptions.ClientException;
import com.decent.common.enums.ErrorCodeEnum;
import com.decent.common.enums.sms.SmsErrorCodeEnum;
import com.decent.common.exception.ErrorCodeException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 阿里滑动验证码对象
 *
 * @author wangyx
 */
@Slf4j
@Data
public class IAcsBean {
    private static final int SUCCESS_CODE = 100;
    private String sessionId;
    private String sig;
    private String token;
    private String scene;

    public void verify(String userIp, String appKey, IAcsClient iAcsClient) throws ClientException {
        AuthenticateSigRequest request = new AuthenticateSigRequest();
        request.setSessionId(sessionId);
        request.setSig(sig);
        request.setToken(token);
        request.setScene(scene);
        request.setAppKey(appKey);
        request.setRemoteIp(userIp);
        AuthenticateSigResponse response = iAcsClient.getAcsResponse(request);
        if (response.getCode() != SUCCESS_CODE) {
            log.warn("[{}]阿里滑动验证失败", response);
            throw new ErrorCodeException(SmsErrorCodeEnum.INVALID_SLIDE_VERIFY_CODE);
        }
    }

    public void paramVerify() {
        if (ObjectUtil.hasNull(sessionId, sig, token, scene)) {
            throw new ErrorCodeException(ErrorCodeEnum.INVALID_PARAMS);
        }
    }
}
